﻿using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Linq;
using System.Globalization;
using VideoSeller.Business.Data;

namespace VideoSeller.Business.Models
{
    
	public class SettingsModel : Repository<Setting>
	{
        private static CultureInfo _cinfo = null;
        public static CultureInfo cinfo
        {
            get
            {
                if (_cinfo == null)
                {
                    _cinfo = new CultureInfo("en-US");
                    _cinfo.NumberFormat.NumberDecimalSeparator = ".";
                }
                return _cinfo;
            }
        }
		public Setting GetSetting(string key)
		{
            Setting item = null;
            try
            {
                item = Table.SingleOrDefault(x => x.KeyName == key);
            }
            catch
            {
                
            }
            if (item == null)
                item = Table.SingleOrDefault(x => x.KeyName == key);
			return item;
		}
        public Dictionary<int, float> getSetIntFloat(string setName, string def)
        {
                Dictionary<int, float> ret = new Dictionary<int, float>();
                try
                {
                    var setting = GetSetting(setName);
                    String s = setting == null ? def : setting.Value;
                    if (String.IsNullOrEmpty(s))
                        s = def;
                    String[] sall = s.Split('|');
                    int np = 0;
                    float f = 1.0F;
                    string[] v = null;
                    foreach (String se in sall)
                    {
                        v = se.Split('-');
                        if (v.Length == 2)
                            if (int.TryParse(v[0], out np) && float.TryParse(v[1].Replace(",","."),NumberStyles.Float, cinfo, out f))
                                ret.Add(np, f);
                    }
                }
                catch
                {
                }
                return ret;
        }
        public Dictionary<string, float> getSetStringFloat(string setName, string def)
        {
            Dictionary<string, float> ret = new Dictionary<string, float>();
            try
            {
                var setting = GetSetting(setName);
                String s = setting == null ? def : setting.Value;
                if (String.IsNullOrEmpty(s))
                    s = def;
                String[] sall = s.Split('|');
                float f = 1.0F;
                string[] v = null;
                foreach (String se in sall)
                {
                    v = se.Split('-');
                    if (v.Length == 2)
                        if (float.TryParse(v[1].Replace(",", "."), NumberStyles.Float, cinfo, out f))
                            ret.Add(v[0], f);
                }
            }
            catch
            {
            }
            return ret;
        }
        public Dictionary<int, float> MaxViewerCntMult
        {
            get
            {
                return getSetIntFloat("MaxViewerCntMult", "5-1");
            }
        }
        public Dictionary<int, float> MaxTimeMult
        {
            get
            {
                return getSetIntFloat("MaxTimeMult", "1-1");
            }
        }
        public Dictionary<string, float> ContentTypeMult
        {
            get
            {
                return getSetStringFloat("ContentTypeMult", "Free-1");
            }
        }
		public List<int> GetMembersCountList()
		{
			var setting = GetSetting("MeetingMembersCountList");
			return setting.Value.Split(new[] {","}, StringSplitOptions.RemoveEmptyEntries).Select(x => int.Parse(x)).ToList();
		}
        public IEnumerable<Setting> QualityStrings
        {
            get
            {
                List<Setting> ret = new List<Setting>();
                float f = -999.999F;
                foreach (Setting cp in _QualityStrings)
                {
                    f = CostForPar(cp.KeyName);
                    if (f != -999.999F)
                        ret.Add(cp);
                }
                return ret;

            }
        }
        public String getQualityString(String name)
        {
           Setting s= Table.Where(m => m.KeyName == name).FirstOrDefault();
           if (s != null)
               return s.Value;
           else
               return "";
        }
        private IEnumerable<Setting> _QualityStrings
        {
            get
            {
                return Table.Where(m => m.KeyName.Contains("CamSet")).OrderBy(m => m.KeyName);
            }
        }
        public Dictionary<string, float> QualityStringCosts
        {
            get
            {
                Dictionary<string, float> ret = new Dictionary<string, float>();
                float f = 0.0F;
                foreach (Setting cp in _QualityStrings)
                {
                    f = CostForPar(cp.KeyName);
                    if (f != -999.999F)
                       ret.Add(cp.KeyName,f);
                }
                return ret;
            }
        }
        public float CostForPar(string par)
        {
            float f = 0.0F;
            Setting se = CamCosts.Where(m => m.KeyName == par.Replace("CamSet", "CamCost")).FirstOrDefault();
            if (se != null)
                if (float.TryParse(se.Value.Replace(",", "."), NumberStyles.Float, cinfo, out f))
                    return f;
            return -999.999F;
        }
        public IEnumerable<Setting> CamCosts
        {
            get
            {
                return Table.Where(m => m.KeyName.Contains("CamCost")).OrderBy(m => m.KeyName);
            }
        }
        public Setting findQualityString(String value)
        {
            return QualityStrings.Where(m => m.KeyName == value).FirstOrDefault();
        }
		public void UpdateSetting(int id, string value, string description)
		{
			var item = Table.SingleOrDefault(x => x.Id == id);

			if (item != null)
			{
				item.Value = value;
				item.Description = description;
				SubmitAllChanges();
			}
		}
	}
}